package com.vcredit.creditcat.manager.emall;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.emall.JdProcessService;
import com.vcredit.creditcat.api.IJdProcessManager;
import com.vcredit.creditcat.dto.emallcredit.JdProcessInDto;
import com.vcredit.creditcat.enumtype.EMStatusEnum;
import com.vcredit.creditcat.model.AuthJd;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.service.AuthJdService;
import com.vcredit.creditcat.util.Constants;
import com.vcredit.framework.annotation.Log;

@Service(protocol = { "protocol1" }, version = "1.0")
public class JdProcessManager implements IJdProcessManager {
	@Autowired
	private JdProcessService jdProcessService;
	@Autowired
	private AuthJdService authJdService;

	@Override
	@Log
	public void login(Cust cust, JdProcessInDto jDProcessInDto) throws Exception {

		BirdResponse initResult = jdProcessService.jdCookieInit();
		String token = initResult.getToken();
		jdProcessService.jdCookieLogin(token, cust.getCustIdno(), cust.getCustName(), jDProcessInDto.getUserName(),
				jDProcessInDto.getPassword(), jDProcessInDto.getCookies(), Constants.JD_BUSTYPE, null);
		AuthJd authJd = new AuthJd();
		authJd.setToken(token);
		authJd.setProcessId(cust.getLastProccessId());
		authJd.setAuthStatus(EMStatusEnum.CRAWING.getCode());
		authJd.setExpiredTime("");
		authJd.setCust(cust);
		// 保存信息
		authJdService.updateCreditJd(authJd);

	}

	@Log
	public void updateCrawlStatus(AuthJd authJd) throws Exception {
		authJdService.updateCreditJd(authJd);

	}

	public List<AuthJd> crawlJd() {
		List<AuthJd> authJdList = authJdService.findUnCrawl();
		return authJdList;
	}
}
